﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chowpoo.Sql;
using CourseSystem.Core.Entities;
using CourseSystem.Core.SqlServerDAL;

namespace CourseSystem.Core.BLL
{
    public class StudentBLL : CommonBLL<StudentInfo, StudentDAL>
    {
        public static IQueryable<StudentInfo> GetList(int collegeId, int departId, int subjectId, int classId, int yearId, string uid, string name, int isScore = -1, int isReviewPass = -1)
        {
            var re = GetList();
            if (yearId != 0)
                re = re.Where(o => o.GradeYear == yearId);
            if (collegeId != 0)
                re = re.Where(o => o.ClassInfo.SubjectInfo.DepartmentInfo.CollegeId == collegeId);
            if (departId != 0)
                re = re.Where(o => o.ClassInfo.SubjectInfo.DepartId == departId);
            if (subjectId != 0)
                re = re.Where(o => o.ClassInfo.SubjectId == subjectId);
            if (classId != 0)
                re = re.Where(o => o.ClassId == classId);
            if (!string.IsNullOrEmpty(uid))
                re = re.Where(o => o.Uid.Contains(uid));
            if (!string.IsNullOrEmpty(name))
                re = re.Where(o => o.StuName.Contains(name));
            if (isScore == 0)
                re = re.Where(o => o.Comment!="");
            else if (isScore == 1)
                re = re.Where(o => o.Comment=="");
            if (isReviewPass != -1)
                re = re.Where(o => o.RegType == isReviewPass);
            return re;
        }

        public static StudentInfo Get(int id)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                var re = dal.SelectSingle(o => o.Id == id);
                return re;
            }
        }

        public static void UpdateLastUrl(int id, string url)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                dal.UpdateLastUrl(id, url);
            }
        }

        public static void UpdateOnlineTime(int id, int time)
        {
            var info = Get(id);
            if (info != null)
            {
                info.OnlineTime += time;
                Edit(info);
            }
        }

        public static void SetLogout(int id)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                dal.LogoutSet(id);
            }
        }

        public static bool ChangePass(int id, string oldPass, string newPass)
        {
            var info = Get(id);
            if (info.PassWord != oldPass)
                return false;
            info.PassWord = newPass;
            return Edit(info);
        }

        public static bool IsExist(string uid, int id = 0)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.SelectSingle(o => o.Id != id && o.Uid == uid) != null;
            }
        }

        public static StudentInfo Login(string uid, string pass)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.SelectSingle(o => o.Uid == uid && o.PassWord == pass);
            }
        }

        public static bool Del(string[] ids)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.Delete(o => ids.Contains(o.Id.ToString())) != null;
            }
        }

        public static bool Review(string ids, int type)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.Review(ids, type);
            }
        }

        public static StudentInfo GetLastStu(int year, int subjectId)
        {
            using (var dal = new StudentDAL(Settings.ConnectionName))
            {
                return dal.GetLastStu(year, subjectId);
            }
        }

    }
}
